package com.test.controller;

import com.test.entity.RestBean;
import com.test.entity.vo.request.EmailAccountVo;
import com.test.entity.vo.request.ResetConfirmVo;
import com.test.entity.vo.request.ResetPasswordVo;
import com.test.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("/api/auth")
public class AuthorizeController {
    @Resource
    AccountService accountService;
        @GetMapping("/ask_code")
    public RestBean<Void> askCode(@RequestParam @Pattern(regexp = "(register|reset|Modify)") String type,
                            @RequestParam @Email String email,
                            HttpServletRequest request){
        String message = accountService.registerEmailVerifyCode(type,email,request.getRemoteAddr());
                if(message == null){
                    return RestBean.success();
                }else{
                    return RestBean.failure(400,message);
                }
            }
        @PostMapping("/register")
    public RestBean<Void> register(@RequestBody @Valid EmailAccountVo vo){
        String message = accountService.registerEmailAccount(vo);
        if(message == null){
            return RestBean.success();
        }else{
            return RestBean.failure(400,message);
        }
    }
    @PostMapping("/resetConfirm")
    public RestBean<Void> resetConfirm(@RequestBody @Valid ResetConfirmVo vo){
        String message = accountService.resetConfirm(vo);
        if(message == null){
            return RestBean.success();
        }else{
            return RestBean.failure(400,message);
        }
    }
    @PostMapping("/resetPassword")
    public RestBean<Void> resetPassword(@RequestBody @Valid ResetPasswordVo vo){
        String message = accountService.resetPassword(vo);
        if(message == null){
            return RestBean.success();
        }else{
            return RestBean.failure(400,message);
        }
    }

}
